﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class LexiconController
    {
        private SQLiteFactory sqlf;
        private SQLite.SQLiteConnection conn;
        private List<Lexicon> list;
        private Random rand;

        public LexiconController()
        {
            sqlf = new SQLiteFactory();
            rand = new Random();
        }
        /// <summary>
        /// Returns synonyms for relations and concepts
        /// </summary>
        /// <param name="name"></param>
        /// <param name="table">Lexicon_Concept for concepts; Lexicon_Relation for relations</param>
        /// <returns></returns>
        public String getSynonym(String name, String table)
        {
            String str = "";
            list = new List<Lexicon>();
            conn = sqlf.getConnection();

            if(table.Equals("Lexicon_Concept"))
                list = conn.Query<Lexicon>("Select * from Lexicon_Concept where conceptName='" + name + "'").ToList();
            else
                list = conn.Query<Lexicon>("Select * from Lexicon_Relation where relationName='" + name + "'").ToList();
            
            try
            {
                str = list[rand.Next(list.Count)].synonym;
            }
            catch (Exception ex)
            {
                str = name;
                //System.Diagnostics.Debug.WriteLine("Error: lexicon null");
            }
            conn.Close();
            return  str;
        }

    }
}
